
#include "process_msg_db.h"
#include "player_mgr.h"
#include "zone_server_mgr.h"

#include <server_share/def_db.h>
#include <server_share/msg_struct_zone.h>
#include <server_share/record_player.h>
#include <server_share/server_def.h>

using namespace DEF;
using namespace DEF_DB;
using namespace NLNET;
using namespace EGS;
using namespace MSG_STR;

////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// SERVICE IMPLEMENTATION /////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////

void cbGetPlayerSuccess  (NLNET::CMessage &msgin, const std::string &serviceName, NLNET::TServiceId  sid)
{
	DB_RECORD::CRecordPlayer record_player;
	msgin.serial(record_player);

	CPlayer* pPlayer = PlayerMgr.findPlayer(record_player.uid);
	if ( pPlayer != NULL )
	{
		//CZoneService* pZoneService = ZoneServiceMgr.findFreeScene( scene );
		TServiceId zoneSId = ZoneServiceMgr.findFreeScene( record_player.scene );

		if ( zoneSId != NLNET::TServiceId::InvalidId )
		{
			pPlayer->conZS = zoneSId;

			//CMessage msgout("INIT_PLAYER");
			//msgout.serial(record_player);
			//msgout.serial(pPlayer->conFES);
			msgin.invert();
			msgin.serial(pPlayer->conFES);
			Network->send(zoneSId,msgin);

			//CMessage msgout("LOAD_SCENE");
			//msgout.serial(uid);
			//msgout.serial(scene);
			//msgout.serial(zoneSId);
			//Network->send(pPlayer->conFES,msgout);
		}
		else
		{
			sendERR( SERVER_FULL, record_player.uid, pPlayer->conFES );		// to FES
			PlayerMgr.removePlayer(pPlayer);
		}
	}
	//sendERR( PLAYER_LOAD_ERROR, uid, sid );		// to PDS
}

void cbGetPlayerFail  (NLNET::CMessage &msgin, const std::string &serviceName, NLNET::TServiceId  sid)
{
	UID      uid;
	msgin.serial(uid);

	CPlayer* pPlayer = PlayerMgr.findPlayer(uid);
	if ( pPlayer != NULL )
	{
		CMessage msgout("PLAYER_NULL");
		msgout.serial(uid);
		Network->send(pPlayer->conFES,msgout);
	}
}



